<?php

class PelaporanController extends Controller
{
    public $layout='//layouts/column2';

    protected function getIndexList($model) {
        $indexList = array(
            array(
		'name' => 'nama',
		'value' => '($data->pelaporan) ? $data->pelaporan->keterangan : "-"',
	),array(
		'name' => 'status_kelengkapan',
                'value'=>'($data->status_kelengkapan) ? $data->status_kelengkapan : "-"'
	),array(
                'htmlOptions' => array('nowrap'=>'nowrap','class'=>'button-column'),
                'class'=>'booster.widgets.TbButtonColumn',
                'template'=>'{view}',
                'buttons'=>array(
                    'view'=>array(
                        'visible'=>'(!Yii::app()->user->isGuest && (!empty($data->upload_dokumen))) ? true : false',
                        'url'=>'array("checklistPelaporan/detail/".$data->id)',
                    ),
                )
            ));
        return $indexList;
    }

    protected function getViewList($model) {
        $viewList = array(
            array(
	'name' => 'nama',
	'value' => ($model->nama) ? $model->nama : "-",
),array(
	'name' => 'keterangan',
	'value' => ($model->keterangan) ? $model->keterangan : "-",
),array(
	'name' => 'upload_dokumen',
	'value' => ($model->upload_dokumen) ? $model->upload_dokumen : "-",
),array(
	'name' => 'created',
	'value' => ($model->created) ? $model->created : "-",
),array(
	'name' => 'modified',
	'value' => ($model->modified) ? $model->modified : "-",
),array(
	'name' => 'perusahaan_id',
	'value' => ($model->Perusahaan) ? $model->Perusahaan->id : "-",
),        );
        return $viewList;
    }
    
    public function actionDetail($id) {
        $model=new CActiveDataProvider('ChecklistPelaporan', array(
            'criteria'=>array(
                'condition'=>'izin_id='.$id,     
                ),
            )
       );
        $data = Izin::model()->findByPk($id);
        $this->render('detail', array(
            'model' => $model,
            'data'=>$data,
        ));
    }

    /**
    * Displays a particular model.
    * @param integer $id the ID of the model to be displayed
    */
    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }

    public function actionCreate()
    {
        $model=new Pelaporan;
        $this->performAjaxValidation($model);
        if(isset($_POST['Pelaporan']))
        {
            $model->attributes=$_POST['Pelaporan'];
            if($model->save())
                $this->redirect(array($this->defaultAction));
        }
        $this->render('create',array(
            'model'=>$model,
        ));
    }

    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
        $this->performAjaxValidation($model);
        if(isset($_POST['Pelaporan']))
        {
            $model->attributes=$_POST['Pelaporan'];
            if($model->save())
                $this->redirect(array($this->defaultAction));
        }
        $this->render('update',array(
            'model'=>$model,
        ));
    }

    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            $this->loadModel($id)->delete();
            if(!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
            }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

    public function actionIndex()
    {
        $model=new Pelaporan('publik');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Pelaporan']))
            $model->attributes=$_GET['Pelaporan'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    /**
    * Manages all models.
    */
    public function actionAdmin()
    {
        $model=new Pelaporan('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Pelaporan']))
            $model->attributes=$_GET['Pelaporan'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    public function loadModel($id)
    {
        $model=Pelaporan::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='pelaporan-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
